<template>
    <div class="contentmain">
        <el-row>
            <el-col :span="7" style="float:left">
                <el-button type="primary" icon="el-icon-plus" v-if="!$store.state.shiro" @click="newadd2">添加入库单</el-button>
                <el-button type="primary" @click="newadd">导入</el-button>
                <el-button type="primary" @click="out">导出</el-button>
                <el-button type="primary" @click="download">模板下载</el-button>
            </el-col>
            <el-col :span="17" style="float:right">
                <el-form :inline="true" :model="formInline" class="demo-form-inline">
                    <el-form-item style="float:right">
                        <el-button type="primary" @click="onSubmit">查询</el-button>
                    </el-form-item>
                    <el-form-item style="float:right;width:140px">
                      <el-select v-model="formInline.status" placeholder="审核状态">
                        <el-option
                          v-for="item in options"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item style="float:right;width:140px">
                        <el-input v-model="formInline.supplier" placeholder="供应商(档口号)"></el-input>
                    </el-form-item>
                    <!-- <el-form-item style="float:right">
                        <el-cascader
                            :options="caigou"
                            v-model="formInline.city"
                            @change="handleChange"
                            clearable
                            placeholder="采购渠道">
                        </el-cascader>
                    </el-form-item> -->
                    <el-form-item style="float:right">
                        <el-date-picker
                            v-model="formInline.value1"
                            type="datetimerange"
                            range-separator="至"
                            start-placeholder="入库日期" :default-time="['00:00:00', '23:59:59']">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item style="float:right;width:140px">
                        <el-input v-model="formInline.stockid" placeholder="商品编号"></el-input>
                    </el-form-item>
                    <el-form-item style="float:right;width:140px">
                        <el-input v-model="formInline.gna" placeholder="商品名称"></el-input>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-table ref="multipleTable" :data="td" tooltip-effect="dark" style="width: 100%" v-loading="loading" element-loading-text="数据加载中...">
                    <el-table-column label="门店">
                        <template slot-scope="scope">
                            {{shopname(scope.row.shopId)}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="goodId" label="商品编号">
                    </el-table-column>
                    <el-table-column prop="goodName" label="商品名称">
                    </el-table-column>
                    <el-table-column label="采购时间">
                        <template slot-scope="scope">
                            {{scope.row.buyTime | format}}
                        </template>
                    </el-table-column>
                    <el-table-column label="入库时间">
                        <template slot-scope="scope">
                            {{scope.row.createTime | format}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="buyNum" label="采购量">
                    </el-table-column>
                    <el-table-column prop="spec" label="单位">
                    </el-table-column>
                    <el-table-column prop="price" label="单价/元(成本价)">
                    </el-table-column>
                    <el-table-column prop="allPrice" label="总金额/元(成本价)">
                    </el-table-column>
                    <el-table-column prop="buyAddress" label="采购渠道">
                    </el-table-column>
                    <el-table-column prop="supplier" label="供应商">
                    </el-table-column>
                    <el-table-column prop="remark" label="备注">
                    </el-table-column>
                    <el-table-column label="审核状态">
                        <template slot-scope="scope">
                            {{scope.row.status==0?'等待审核':(scope.row.status==1?'审核成功':'审核失败')}}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <el-button v-if="!$store.state.shiro || scope.row.status!=0" size="mini" @click="detial(scope.$index, scope.row)">查看更多</el-button>
                            <el-button v-if="$store.state.shiro && scope.row.status==0" size="mini" @click="checkGoodsStock(scope.row.id,1)">通过</el-button>
                            <el-button v-if="$store.state.shiro && scope.row.status==0" size="mini" @click="checkGoodsStock(scope.row.id,2)">拒绝</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <CreatePutInStorage />
        <CreatePutInStorage2 />
        <el-row>
            <el-col :span="8" :offset="16">
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="10"
                layout="total, prev, pager, next, jumper"
                :total="pagecount">
                </el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import moment from 'moment'
import CreatePutInStorage from 'components/Drp/CreatePutInStorage'
import CreatePutInStorage2 from 'components/Drp/CreatePutInStorage2'
export default{
    data(){
        return{
            formInline:{},
            td:[],
            currentPage:1,
            pagecount:0,
            loading:false,
            opt:[],
            caigou:[],
            sheng:0,
            shi:0,
            cities:[],
            items:[{
                shopName:'全部店铺',
                shopId:-1
            }],
            options:[{
                value: '-1',
                label: '全部'
            },{
                value: '0',
                label: '等待审核'
            }, {
                value: '1',
                label: '审核成功'
            }, {
                value: '2',
                label: '审核失败'
            }]
        }
    },
    components:{
        CreatePutInStorage,
        CreatePutInStorage2
    },
    filters:{
        format(v){
            return moment(v).format("YYYY-MM-DD")
        }
    },
    computed:{
        getstate(){
            return this.$store.state.TOPshopID;
        },
        getdialog(){
            return this.$store.state.createruku;
        }
    },
    watch:{
        getstate(v){
            if(v){
                var pp=new URLSearchParams()
                if(!this.$store.state.shiro){
                    return
                }
                if(this.formInline.gna){
                    pp.append('goodName',this.formInline.gna)
                }
                if(this.formInline.stockid){
                    pp.append('goodId',this.formInline.stockid)
                }
                if(this.formInline.value1){
                    pp.append('buyTime',moment(this.formInline.value1[0]).format("YYYY-MM-DD HH:mm:ss"))
                    pp.append('buyEndTime',moment(this.formInline.value1[1]).format("YYYY-MM-DD HH:mm:ss"))
                }
                if(this.formInline.supplier){
                    pp.append('supplier',this.formInline.supplier)
                }
                if(this.$store.state.TOPshopID!=-1){
                    pp.append('shopId',this.$store.state.TOPshopID)
                }
                if(this.formInline.status && this.formInline.status!=-1){
                    pp.append('status',this.formInline.status)
                }
                this.getData(pp)
            }
        },
        getdialog(v){
            if(!v){
                this.$store.state.createruku=true
                if(this.$store.state.shiro){
                    this.getData()
                }else{
                    if(this.$store.state.TOPshopID){
                        var pp=new URLSearchParams()
                        pp.append('shopId',this.$store.state.TOPshopID)
                        this.getData(pp)
                    }
                }
            }
        }
    },
    activated(){
        if(isDetailBack){
            if(this.$store.state.shiro){
                this.getData()
            }else{
                if(this.$store.state.TOPshopID){
                    var pp=new URLSearchParams()
                    pp.append('shopId',this.$store.state.TOPshopID)
                    this.getData(pp)
                }
            }
        }
    },
    beforeCreate(){
        var _this=this
        this.opt=[]
        axios.get('/shop/listShop.do').then(r=>{//请求店铺列表
            if(r.data.code!=200){
                _this.$message.error(r.data.message)
                return
            }
            _this.opt=[].concat(r.data.response.shopList)
        })

        this.caigou=[]
        axios.get('/listProvince.do').then(r=>{//请求省市区的接口
            if(r.data.code!=200){
                return
            }
            _this.cities=[].concat(r.data.response.provinces)
            var array = r.data.response.provinces
            function clone(o) {//将省市区转为组件可读的数组对象
                var temp = {}
                for (var key in o) {
                    if (Object.prototype.toString.call(o[key])=='[object Array]') {
                        temp['children'] = new Array()
                        for (var i of o[key]) {
                            temp['children'] .push(clone(i))
                        }
                    } else {
                        if (key.indexOf('Id') != -1) {
                            if(!temp['value']) temp['value'] = o[key]
                        } else if (key.indexOf('Name') != -1) {
                            temp['label'] = o[key]
                        }
                    }
                }
                return temp
            }
            for (let i of array ) {
                _this.caigou.push(clone(i))
            }
        })
    },
    methods:{
        download(){//下载模板
            window.open('http://p2sgmzh9e.bkt.clouddn.com/%E5%85%A5%E5%BA%93%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx')
        },
        shengs(v){//获取省的名称
            for(var i=0;i<this.cities.length;i++){
                if(parseInt(v)==this.cities[i].provId){
                    return this.cities[i].provName
                }
            }
        },
        shis(v){//获取市的名称
            for(var i=0;i<this.cities.length;i++){
                for(var j=0;j<this.cities[i].cities.length;j++){
                    if(parseInt(v)==this.cities[i].cities[j].cityId){
                        return this.cities[i].cities[j].cityName
                    }
                }
            }
        },
        shopname(v){//将shopId转为店铺名称
            for(var i=0;i<this.opt.length;i++){
                if(v==this.opt[i].shopId){
                    return this.opt[i].shopName
                }
            }
        },
        handleChange(v){//省市区的获取
            // console.log(v)
            this.sheng=v[0]
            this.shi=v[1]
        },
        handleSizeChange(i){//翻页
            this.topage(i)
        },
        handleCurrentChange(i){//翻页
            this.topage(i)
        },
        topage(i){//翻页带所有参数
            var pp=new URLSearchParams()
            pp.append('pageNo',i)
            if(this.formInline.gna){
                pp.append('goodName',this.formInline.gna)
            }
            if(this.formInline.stockid){
                pp.append('goodId',this.formInline.stockid)
            }
            if(this.formInline.value1){
                pp.append('buyTime',moment(this.formInline.value1[0]).format("YYYY-MM-DD HH:mm:ss"))
                pp.append('buyEndTime',moment(this.formInline.value1[1]).format("YYYY-MM-DD HH:mm:ss"))
            }
            if(this.formInline.supplier){
                pp.append('supplier',this.formInline.supplier)
            }
            if(this.$store.state.TOPshopID && this.$store.state.TOPshopID!=-1){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            if(this.formInline.status && this.formInline.status!=-1){
                pp.append('status',this.formInline.status)
            }
            // if(this.sheng && this.shi){
            //     pp.append('buyPro',this.sheng)
            //     pp.append('buyCity',this.shi)
            // }
            this.getData(pp)
        },
        detial(i,r){//跳转详情页
            this.$router.push({path: '/Detial', query: {id: r.goodId,type:1}})
        },
        onSubmit(){//搜索
            var pp=new URLSearchParams()
            if(this.formInline.gna){
                pp.append('goodName',this.formInline.gna)
            }
            if(this.formInline.stockid){
                pp.append('goodId',this.formInline.stockid)
            }
            if(this.formInline.value1){
                pp.append('buyTime',moment(this.formInline.value1[0]).format("YYYY-MM-DD HH:mm:ss"))
                pp.append('buyEndTime',moment(this.formInline.value1[1]).format("YYYY-MM-DD HH:mm:ss"))
            }
            if(this.formInline.supplier){
                pp.append('supplier',this.formInline.supplier)
            }
            if(this.$store.state.TOPshopID && this.$store.state.TOPshopID!=-1){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            if(this.formInline.status && this.formInline.status!=-1){
                pp.append('status',this.formInline.status)
            }
            this.getData(pp)
        },
        newadd(){//导入的弹窗打开代码
            this.$store.commit('ChangeCreatePutInStorage')
        },
        newadd2(){//添加的弹窗打开代码
            this.$store.commit('ChangeCreatePutInStorage2')
        },
        out(){//导出
            var _this=this
            var pp=new URLSearchParams()
            if(this.formInline.gna){
                pp.append('goodName',this.formInline.gna)
            }
            if(this.formInline.stockid){
                pp.append('goodId',this.formInline.stockid)
            }
            if(this.formInline.value1){
                pp.append('buyTime',moment(this.formInline.value1[0]).format("YYYY-MM-DD HH:mm:ss"))
                pp.append('buyEndTime',moment(this.formInline.value1[1]).format("YYYY-MM-DD HH:mm:ss"))
            }
            if(this.formInline.supplier){
                pp.append('supplier',this.formInline.supplier)
            }
            if(this.$store.state.TOPshopID && this.$store.state.TOPshopID!=-1){
                pp.append('shopId',this.$store.state.TOPshopID)
            }
            if(this.formInline.status && this.formInline.status!=-1){
                pp.append('status',this.formInline.status)
            }
            window.open(HOST+'/getGoodStockByExcel.do?'+pp.toString())

        },
        getData(v){//获取数据
            var _this=this
            this.loading=true
            axios.get('/findGoodStockList.do',{params:v}).then(r=>{
                // console.log('findGoodStockList',r)
                if(r.data.code==200){
                    _this.td=[].concat(r.data.response.goodStockList)
                    _this.pagecount=r.data.response.page.totalCount
                    _this.loading=false
                }else{
                    if(r.data.message=="not login"&&r.data.code==500){
                        _this.$message.error("访问者,请登录")
                        router.push('/Login');
                        return
                    }
                    _this.$message.error(r.data.message)
                    _this.loading=false
                }
            })
        },
        checkGoodsStock(id,type){//检查配置库存是否通过
            var _this=this
            this.loading=true
            let params = {
                goodStockId: id,
                type: type
            }
            axios.get('/checkGoodsStock.do',{params:params}).then(r=>{
                if(r.data.code==200){
                    _this.$message.success('操作成功')
                    _this.handleCurrentChange(_this.currentPage)
                    _this.loading=false
                }else{
                    if(r.data.message=="not login"&&r.data.code==500){
                        _this.$message.error("访问者,请登录")
                        router.push('/Login');
                        return
                    }
                    _this.$message.error(r.data.message)
                    _this.loading=false
                }
            })
        }
    }
}
</script>
<style>

</style>
